/**
 * 
 */
package org.swing.utility.jai.imgio;

import java.awt.AlphaComposite;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

/**
 * @author Quynh Nhu
 *
 */
public class ImageIoCrop {
	private File imageFile;  
    private BufferedImage image;  
  
    public ImageIoCrop(File imageFile)  
    {  
        try  
        {  
            this.imageFile = imageFile;  
            image = ImageIO.read(imageFile);  
        }  
        catch (IOException ex)  
        {  
           ex.printStackTrace();
        }  
    }  
  
    public ImageIoCrop(File imageFile, BufferedImage image)  
    {  
        this.imageFile = imageFile;  
        this.image = image;  
    }  
  
  
    public File getImageFile()  
    {  
        return imageFile;  
    }  
  
    public void setImageFile(File imageFile)  
    {  
        this.imageFile = imageFile;  
    }  
  
    public BufferedImage getImage()  
    {  
        return image;  
    }  
  
    public void setImage(BufferedImage image)  
    {  
        this.image = image;  
    }  
      
    public String getImageExtension()  
    {  
        String fName = imageFile.getName();  
        return fName.substring(fName.indexOf(".")+1, fName.length());  
    }  
    public BufferedImage getResizedCopy(int scaledWidth, int scaledHeight, boolean preserveAlpha)  
    {  
        return getResizedCopyOfImage(image, scaledWidth, scaledHeight, preserveAlpha);  
    }  
      
    public byte[] getByte()  
    {  
        return getByteOfImage(image);  
    }  
    public ImageIcon getImageIcon()  
    {  
        return new ImageIcon(image);  
    }  
    public BufferedImage getCropedImage(int x, int y, int width, int height)  
    {  
        return getCropedImageOfImage(image, x, y, width, height);  
    }  
    public BufferedImage getCropedImageFromCenter(Dimension panelDimension,int clipedWidth, int clipedHeight)  
    {  
        return getCropedImageOfImageFromCenter(image, panelDimension, clipedWidth, clipedHeight);  
    }  
  
    public BufferedImage getResizedCopyOfImage(BufferedImage originalImage,int scaledWidth, int scaledHeight, boolean preserveAlpha)  
    {  
        int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;  
        BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);  
        Graphics2D g = scaledBI.createGraphics();  
        if (preserveAlpha)  
        {  
                g.setComposite(AlphaComposite.Src);  
        }  
        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,  
        RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
        g.setRenderingHint(RenderingHints.KEY_RENDERING,  
        RenderingHints.VALUE_RENDER_QUALITY);  
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,  
        RenderingHints.VALUE_ANTIALIAS_ON);  
        g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);  
        g.dispose();  
        return scaledBI;  
    }  
  
    public byte[] getByteOfImage(BufferedImage sourceImage)  
    {  
        try  
        {  
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
            ImageIO.write(sourceImage, getImageExtension(), baos);  
            baos.flush();  
            byte[] imageData = baos.toByteArray();  
            baos.close();  
            return imageData;  
        }  
        catch (IOException ex)  
        {  
        	 ex.printStackTrace();
            return null;  
        }  
    }  
  
    public ImageIcon getImageIconFromImage(BufferedImage sourceImage)  
    {  
        return new ImageIcon(sourceImage);  
    }  
  
    public BufferedImage  getCropedImageOfImage(BufferedImage sourceImage, int x, int y, int width, int height)  
    {  
        return sourceImage.getSubimage(x, y, width, height);  
    }  
    public BufferedImage getCropedImageOfImageFromCenter(BufferedImage sourceImage, Dimension panelDimension,int clipedWidth, int clipedHeight)  
    {  
        Rectangle clip = new Rectangle(clipedWidth, clipedHeight);  
        clip.x = (panelDimension.width - clip.width) / 2;  
        clip.y = (panelDimension.height - clip.height) / 2;  
        int x0 = (panelDimension.width - sourceImage.getWidth()) / 2;  
        int y0 = (panelDimension.height - sourceImage.getHeight()) / 2;  
        int x = clip.x - x0;  
        int y = clip.y - y0;  
        return sourceImage.getSubimage(x, y, clip.width, clip.height);  
    } 
}
